package com.itchu.system.controller;

import com.itchu.common.domain.PageResult;
import com.itchu.system.domain.SysUser;
import com.itchu.system.dto.SysUserDto;
import com.itchu.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/member")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查找系统用户的分页信息
     * @param key   搜索条件
     * @param page  当前页
     * @param rows  页大小
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<SysUser>> findByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc
    ) {
        PageResult<SysUser> list = sysUserService.findByPage(key,page,rows,sortBy,desc);
        return ResponseEntity.ok(list);
    }

    /**
     * 添加系统用户
     * @param sysUser  系统用户信息
     * @param rids  角色ids
     * @return
     */
    @PostMapping("/save")
    public ResponseEntity<Void> addSysUser(SysUser sysUser, @RequestParam("rids") List<Long> rids) {
        sysUserService.addSysUser(sysUser,rids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 修改系统用户
     * @param sysUser  系统用户信息
     * @param rids  角色ids
     * @return
     */
    @PutMapping("/save")
    public ResponseEntity<Void> updateSysUser(SysUser sysUser, @RequestParam("rids") List<Long> rids) {
        sysUserService.updateSysUser(sysUser,rids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 删除系统用户
     * @param id  系统用户id
     * @return
     */
    @GetMapping("/delete")
    public ResponseEntity<Void> deletSysUser(
            @RequestParam("id")Long id) {

        sysUserService.deletSysUser(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查找系统用户
     * @param id  系统id
     * @return
     */
    @GetMapping("/findById")
    public ResponseEntity<SysUserDto> findById(
            @RequestParam("id")Long id) {
        SysUserDto userDto = sysUserService.findById(id);
        return ResponseEntity.ok(userDto);
    }

}
